package com.xnyzc.lhy.mis.controller.cmsapi.coupon;


import com.xnyzc.lhy.common.entity.CouponRv;
import com.xnyzc.lhy.common.exception.CouponException;
import com.xnyzc.lhy.common.penum.coupon.CouponErrorCode;
import com.xnyzc.lhy.mis.entity.coupon.CouponDefParam;
import com.xnyzc.lhy.mis.entity.coupon.CouponDefSearchResult;
import com.xnyzc.lhy.mis.service.coupon.IOaSysCouponDefService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 优惠券定义 前端控制器
 * </p>
 *
 * @author haoyfa
 * @since 2019-12-16
 */
@RestController
@RequestMapping("/coupon/couponDef")
@Slf4j
public class OaSysCouponDefController {
    @Autowired
    IOaSysCouponDefService iOaSysCouponDefService;

    /**
     * 获取优惠券定义（根据优惠券类型）
     *
     * @return
     */
    @ApiOperation(value = "获取优惠券定义（根据优惠券类型）", notes = "获取优惠券定义（根据优惠券类型）")
    @GetMapping(value = "/queryCouponDefList")
    public CouponRv queryCouponDefList(@RequestParam("defType") String defType) {
        try {
            List<CouponDefSearchResult> couponDefSearchResultList = iOaSysCouponDefService.queryCouponDefList(defType);
            return CouponRv.wrapSuccess(couponDefSearchResultList);
        }
        catch (CouponException ae){
            log.error("获取优惠券定义（根据优惠券类型）" + ae.getErrno().getDesc());
            return CouponRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("获取优惠券定义（根据优惠券类型）" + CouponErrorCode.unKnow.getDesc());
            return CouponRv.wrapErrorCode(CouponErrorCode.unKnow);
        }
    }

    /**
     * 新增优惠券定义
     *
     * @return
     */
    @ApiOperation(value = "新增优惠券定义", notes = "新增优惠券定义")
    @PostMapping(value = "/createCouponDef")
    public CouponRv createCouponDef(@RequestBody CouponDefParam couponDefParam) {
        try {
            iOaSysCouponDefService.createCouponDef(couponDefParam);
            return CouponRv.wrapSuccess();
        }
        catch (CouponException ae){
            log.error("新增优惠券定义 失败：" + ae.getErrno().getDesc());
            return CouponRv.wrapErrorCode(ae.getErrno());
        }
        catch (Exception ex){
            log.error("新增优惠券定义 失败：" + CouponErrorCode.unKnow.getDesc());
            return CouponRv.wrapErrorCode(CouponErrorCode.unKnow);
        }
    }
}
